home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / disk / mutex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.3 KB  |  206 lines

  1. /*
  2.  *   $RCSfile: mutex.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:40 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37. #include "queue_consist.h"
  38. #include "mutex_compile.h"
  39.  
  40. #include "ess.h"
  41. #include "checking.h"
  42. #include "sysdefs.h"
  43. #include "list.h"
  44. #include "error.h"
  45. #include "trace.h"
  46. #include "tid.h"
  47. #include "pool.h"
  48. #include "io.h"
  49. #include "bitvec.h"
  50. #include "lock.h"
  51. #include "msgdefs.h"
  52. #include "disk.h"
  53. #include "thread.h"
  54. #include "semaphore.h"
  55. #include "latch.h"
  56. #include "bf.h"
  57. #include "link.h"
  58. #include "volume.h"
  59. #include "trace.h"
  60. #include "msgvector.h"
  61. #include "disk_funcs.h"
  62. #include "disk_globals.h"
  63. #include "io_globals.h"
  64. #include "msg_globals.h"
  65. #include "sharedmem_globals.h"
  66.  
  67. #ifdef DEBUG
  68. static int spin_count[7];
  69.  
  70. void
  71. dump_spin()
  72. {
  73.     {     register int i, j;
  74.         for(i=1, j=0; i < (sizeof(spin_count)/sizeof(int)); i++) {
  75.             j+= spin_count[i];
  76.         }
  77.         if(j == 0) /* don't bother to print anything: they're all 
  78.           in the first category */
  79.             return;
  80.     }
  81.     fprintf(stderr,
  82.         "%s: Histogram of number of spins:\n",
  83.             process_identity.me==SERVER_PROC?"SERVER":"DISKRW");
  84.     fprintf(stderr,
  85.         "%8.8s | %8.8s | %8.8s | %8.8s | %8.8s | %8.8s | %8.8s\n",
  86.         "<10",     "<10**3", "<10**4", "<10**5", "<10**6", "<10**7", ">10**7");
  87.     fprintf(stderr,
  88.         "%8d | %8d | %8d | %8d | %8d | %8d | %8d\n",
  89.         spin_count[0], spin_count[1], spin_count[2], spin_count[3],
  90.         spin_count[4], spin_count[5], spin_count[6]);
  91. } /* dump_spin */
  92. #endif DEBUG
  93.  
  94. void
  95. getMutex(
  96.     MUTEX *qp
  97. )
  98. {
  99. #ifdef DEBUG
  100.     register int spins=0;
  101. #endif DEBUG
  102.  
  103. #ifdef TESTANDSET
  104.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in getMutex(0x%x) sync %d",
  105.         process_identity.me==SERVER_PROC?"server":"diskrw", qp,
  106.         qp->sync
  107.         ));
  108. #else TESTANDSET
  109.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in getMutex(0x%x) sync %d",
  110.         process_identity.me==SERVER_PROC?"server":"diskrw", qp,
  111.         (qp)->flag[process_identity.me]
  112.         ));
  113. #endif TESTANDSET
  114.  
  115.     CHECK_MUTEX_MAGIC(qp);
  116. #ifdef TESTANDSET
  117.     while (testandset(&qp->sync, 1) != 0)
  118. #else TESTANDSET
  119.     qp->flag[process_identity.me] = 1; /* want the cs */
  120.     qp->turn = process_identity.other;     /* give him a chance */
  121.     while(qp->flag[process_identity.other] && 
  122.         (qp->turn == process_identity.other)) 
  123. #endif TESTANDSET
  124.     {
  125. #ifdef DEBUG
  126.         spins++;
  127. #endif DEBUG
  128.     } 
  129.  
  130. #ifdef DEBUG
  131.     if(spins < 10) spin_count[0]++;
  132.     else if(spins < 1000) spin_count[1]++;
  133.     else if(spins < 10000) spin_count[2]++;
  134.     else if(spins < 100000) spin_count[3]++;
  135.     else if(spins < 1000000) spin_count[4]++;
  136.     else if(spins < 10000000) spin_count[5]++;
  137.     else spin_count[6]++;
  138.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s got Mutex after %d spins",
  139.         process_identity.me==SERVER_PROC?"server":"diskrw", spins));
  140.  
  141.  
  142. #define MILLION 1000000
  143.     SM_ASSERT(LEVEL_1, (spins < 720*MILLION));
  144.  
  145. #else
  146.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s got Mutex ",
  147.         process_identity.me==SERVER_PROC?"server":"diskrw"));
  148.  
  149. #endif DEBUG
  150.  
  151. } /* getMutex */
  152.  
  153.  
  154. /*
  155.  * returns 1 for success, 0 for failure 
  156.  */
  157. int
  158. tryMutex(
  159.     MUTEX *qp
  160. )
  161. {
  162.     CHECK_MUTEX_MAGIC(qp);
  163. #ifdef TESTANDSET
  164.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in tryMutex(0x%x) sync %d",
  165.         process_identity.me==SERVER_PROC?"server":"diskrw", qp,
  166.             qp->sync));
  167.     return (testandset(&qp->sync, 1) == 0)? 1 : 0;
  168. #else TESTANDSET
  169.     qp->flag[process_identity.me]=1; /* want the cs */
  170.     qp->turn = process_identity.other;     /* give him a chance */
  171.     if(qp->flag[process_identity.other] && 
  172.         (qp->turn == process_identity.other)) {
  173.         qp->flag[process_identity.me]=0; /* don't want it anymore */
  174.         return 0;
  175.     } 
  176.     return 1;
  177. #endif TESTANDSET
  178. } /* tryMutex */
  179.  
  180. void
  181. giveMutex(
  182.     MUTEX *qp
  183. )
  184. {
  185.  
  186. #ifdef TESTANDSET
  187.     qp->sync = 0;
  188. #else TESTANDSET
  189.     (qp)->flag[process_identity.me]=0; /* i'm out of cs & don't want it */ 
  190. #endif TESTANDSET
  191.  
  192. #ifdef TESTANDSET
  193.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s gave up Mutex(0x%x) sync=%d ",
  194.         process_identity.me==SERVER_PROC?"server":"diskrw", qp, 
  195.         qp->sync
  196.         ));
  197. #else TESTANDSET
  198.     TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s gave up Mutex(0x%x) sync=%d ",
  199.         process_identity.me==SERVER_PROC?"server":"diskrw", qp, 
  200.         (qp)->flag[process_identity.me]
  201.         ));
  202. #endif TESTANDSET
  203.     CHECK_MUTEX_MAGIC(qp);
  204. } /* giveMutex */
  205.  
  206.